home *** CD-ROM | disk | FTP | other *** search
- Path: camelot.dsccc.com!not-for-mail
- From: kcline@sun152.spd.dsccc.com (Kevin Cline)
- Newsgroups: comp.lang.c++
- Subject: Re: Why Do Return Values Sometimes Have '&' Appended?
- Date: 22 Feb 1996 15:37:07 -0600
- Organization: DSC Communications Corporation Switch Products Division
- Message-ID: <4ginm3$npa@sun152.spd.dsccc.com>
- References: <4ggciv$iq1@alcor.usc.edu> <4gh3tb$l7n@qualcomm.com>
- NNTP-Posting-Host: sun152.spd.dsccc.com
-
- In article <4gh3tb$l7n@qualcomm.com>,
- Nasser Abbasi <x!news.be.innet.net!INbe.net!usenet> wrote:
- >In article <4ggciv$iq1@alcor.usc.edu>, wawda@alcor.usc.edu (Abu Wawda) says:
- >>
- >>I have seen code where some of the functions are returned with the
- >>reference operator (&) attached to the end. Here is an example:
- >>
- >>MyReturn& MyFunction(...)
- >>{
- >>}
- >>
- >>I don't understand what that does.
- >
- >...
-
- >One possibility is that MyFunction internally has a hidden static
- >object of type MyReturn that processing is done on, then finaly
- >a reference to it is returned.
- >
-
- In general, this is a bad idea. Such code is automatically NOT
- thread-safe. If the internal object is used to hold data from call to
- call, that is even worse; each user of the function must be careful
- somehow not to break some other user. In general objects should hold
- state, not functions.
-
- Of course, the same holds for class static data as well.
-
- However, it is fine for a function to return a reference to one
- of it's arguments, e.g.
-
- ostream& operator<<(ostream& o, const SomeClass& obj) {
- ...
- return o;
- }
-
- It is also common for a member function to return a reference to *this,
- e.g.
-
- SomeClass::operator=(const SomeClass&) {
- ...
- return *this;
- }
-
-
- --
- Kevin Cline
-